package com.shoppingmall;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.shoppingmall.mainfragment.ClassiFicationFragment;
import com.shoppingmall.mainfragment.HomeFragment;
import com.shoppingmall.mainfragment.MineFragment;
import com.shoppingmall.mainfragment.ShopCartFragment;
import com.yinglan.alphatabs.AlphaTabsIndicator;
import com.yinglan.alphatabs.OnTabChangedListner;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;


public class MainActivity extends FragmentActivity {

    private AlphaTabsIndicator tabView;
    private List<Fragment> fgList;
    private Fragment nowFragment;
    private View stausBar;
    private int postion = 0;//记录栈位置

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);解决显示出错的时候frgment可能显示重复

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        tabView = findViewById(R.id.tab_view);
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setStatusBarLightMode(this, false);
    }

    private void initData() {

        fgList = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        ClassiFicationFragment collegeFragment = new ClassiFicationFragment();
        ShopCartFragment shopCartFragment = new ShopCartFragment();
        MineFragment mineFragment = new MineFragment();
        fgList.add(homeFragment);
        fgList.add(collegeFragment);
        fgList.add(shopCartFragment);
        fgList.add(mineFragment);
        nowFragment = homeFragment;
        FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_content, homeFragment, "homeFragment");
        fragmentTransaction.hide(homeFragment);
        fragmentTransaction.show(homeFragment).commit();
    }


    private void initEvent() {
        tabView.setOnTabChangedListner(new OnTabChangedListner() {
            @Override
            public void onTabSelected(int tabNum) {
                switchFragment(nowFragment, fgList.get(tabNum));
                nowFragment = fgList.get(tabNum);
//                if (tabNum == 2 || tabNum == 3) {
//                    if (TextUtils.isEmpty(userId)) {
//                        ToastUtils.showShort(MainActivity.this, "请先登录");
//                        startActivityForResult(new Intent(MainActivity.this, LoginActivity.class), Constants.INTENT_LOGIN_CODE);
//                    }
//                }

            }
        });
    }

    /**
     * 切换Fragment
     * <p>(hide、show、add)
     *
     * @param fromFragment
     * @param toFragment
     */
    private void switchFragment(Fragment fromFragment, Fragment toFragment) {
        hideAllFragment();
        if (nowFragment != toFragment) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!toFragment.isAdded()) {    // 先判断是否被add过
                fragmentTransaction.add(R.id.fl_content, toFragment);
                fragmentTransaction.show(toFragment).commit(); // 隐藏当前的fragment，add下一个到activity中
            } else {
                fragmentTransaction.hide(fromFragment).show(toFragment).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void hideAllFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fgList.size(); i++) {
            if (fgList.get(i) != null)
                fragmentTransaction.hide(fgList.get(i));

        }
        fragmentTransaction.commit();
    }

    //退出app
    @Override
    public void onBackPressed() {
        exit();
    }

    private long exitTime = 0;

    private void exit() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            exitTime = System.currentTimeMillis();
            ToastUtils.showShort("再按一次退出程序");
        } else {
            AppUtils.exitApp();
        }
    }
}
